// packageA/pages/opinion/opinion.js
import {
    wxfileTobase64
} from '../../utils/util.js'
// 111
// import {
//     fastAddInsuranceApi
// } from '../../api/fastAddInsurance'
import {
    suggestion
} from '../../api/app.js'
import user from '../../utils/user.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: 1,
        content: '',
        phone: '',
        typeList: [{
            text: '投诉',
            key: 1
        },
        {
            text: '建议',
            key: 2
        },
        {
            text: '故障',
            key: 3
        },
        {
            text: '其他',
            key: 4
        },
        ],
        fileList: []
    },

    // 111
    // onLoad(options) {
    //     console.log(options)
    //     user.waitLogin().then(async () => {
    //         let res = await fastAddInsuranceApi({
    //             qrProjectId: decodeURIComponent(options.scene).split('=')[1],
    //             staffRecord: {
    //                 userName: user.userName,
    //                 idCardNo: user.cardNo
    //             }
    //         })
    //         console.log(res, 'saffg')
    //         if (res.code === 1) {
    //             this.setData({
    //                 status: 2,
    //                 title: '投保失败',
    //                 msg: res.msg
    //             })
    //         } else {
    //             if (res.record.policyStatus === 0) {
    //                 this.setData({
    //                     status: 1,
    //                     title: '投保申请已提交',
    //                     msg: '您已提交申请，待' + res.record.insurStartTime + '后保险即可生效'
    //                 })
    //             } else if (res.record.policyStatus === 1) {
    //                 this.setData({
    //                     status: 2,
    //                     title: '保障中',
    //                     msg: '投保成功，当前保险已生效'
    //                 })
    //             }
    //             this.setData({
    //                 info: res.record
    //             })
    //         }
    //     })
    // },

    // 切换投诉类别
    checkType(event) {
        this.setData({
            type: event.currentTarget.dataset.key
        })
    },

    // 上传图片
    async upload() {
        const t = this
        const result = await wx.chooseMedia({
            count: 4,
            sizeType: ['compressed'],
            sourceType: ['album', 'camera'],
            mediaType: ['image']
        })
        console.log(result, '数据数据')
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = t.data.fileList
        if ((tempFilePaths.length + result.tempFiles.length) > 4) {
            app.toast('最多只能上传4张图片！')
            return 0
        }
        tempFilePaths.push(...result.tempFiles)
        t.setData({
            fileList: tempFilePaths
        })
        console.log(this.data.fileList, 'fileListfileList')
    },
    inputchange(event) {
        this.setData({
            [event.currentTarget.dataset.model]: event.detail.value
        })
    },
    async submit() {
        // if(!user.userId){
        //     app.toast('请先登录')
        //     return
        // }
        if (!this.data.content) {
            app.toast('请填写投诉内容！')
            return 0
        }
        if (this.data.phone !== '' && !/^1\d{10}$/.test(this.data.phone)) {
            app.toast('请填写正确的手机号码！')
            return 0
        }
        const promiseList = []
        for (const i in this.data.fileList) {
            promiseList.push(wxfileTobase64(this.data.fileList[i].tempFilePath))
        }
        const result = await Promise.all(promiseList)
        await suggestion({
            type: this.data.type,
            userId: user.userId,
            suggestion: this.data.content,
            companyId: user.companyId,
            phone: this.data.phone,
            createUserId: user.companyId,
            params: {
                photos: result
            }
        })
        wx.navigateTo({
            url: '/pages/success/success?title=反馈成功&content=感谢您的反馈，我们会继续加油'
        })
    },





})